/**
 * 
 */
package edu.pw.treegrid.server.classmodel;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import edu.pw.treegrid.server.query.ColumnNode;
import edu.pw.treegrid.server.query.Node;
import edu.pw.treegrid.server.reportmodel.ReportColumn;
import edu.pw.treegrid.server.service.XMLMarshaller;

/** 
 * @author Piotrek
 */
public class SimpleAttribute extends DomainAttribute {

	private static final String EXPRESSION_ELEMENT = "expression";
	private static final String MEASURE_ELEMENT = "measure";
	private String expression;

	public String getExpression() {
		return expression;
	}

	public void setExpression(String expression) {
		this.expression = expression;
	}

	private Boolean measure = false;

	public Boolean isMeasure() {
		return measure;
	}
	
	public Boolean getMeasure() {
		return measure;
	}

	public void setMeasure(Boolean isMeasure) {
		this.measure = isMeasure;
	}

	public Node buildQueryNode(ReportColumn reportColumn) {
		ColumnNode cn = new ColumnNode();
		cn.setExpression(expression);
		cn.setSimpleAttribute(this);
		cn.setReportColumn(reportColumn);
		return cn;
	}

	public SimpleAttribute getSimpleAttribute() {
		return this;
	}

	@Override
	protected void serializeToXML(Document document, Element attributeElement) {
		Element isMeasureElement = document.createElement(MEASURE_ELEMENT);
		isMeasureElement.setTextContent(Boolean.toString(this.measure));
		attributeElement.appendChild(isMeasureElement);
		
		Element expressionElement = document.createElement(EXPRESSION_ELEMENT);
		expressionElement.setTextContent(this.expression);
		attributeElement.appendChild(expressionElement);
	}
	
	@Override
	protected void deserializeFromXML(Element ae) {
		super.deserializeFromXML(ae);
		
		this.measure = Boolean.parseBoolean(getElementsText(ae, MEASURE_ELEMENT));
		this.expression = getElementsText(ae, EXPRESSION_ELEMENT);
	}
	
	@Override
	public void setReferences(XMLMarshaller xmlMarshaller) {
		super.setReferences(xmlMarshaller);
	}
}